/*
 * 
 * v1.2 by tonydon
 */
package cn.txdnet.txcms.service;

import cn.txdnet.key.GenId;
import cn.txdnet.txcms.constants.CacheConst;
import cn.txdnet.txcms.dao.InfobaseDAO;
import cn.txdnet.txcms.domain.Infobase;
import cn.txdnet.txcms.dto.InfoDTO;
import cn.txdnet.txcms.dto.PageDTO;
import cn.txdnet.txcms.query.InfobaseQuery;
import cn.txdnet.txcms.query.ViewInfoQuery;
import cn.txdnet.txweb.basedao.FacadeProxy;
import cn.txdnet.txweb.cache.TxCacheManager;
import cn.txdnet.txweb.utils.CollectionUtil;
import cn.txdnet.txweb.utils.StringUtil;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public class InfobaseService {


    /*
     * 保存infobase
     */
    public static boolean make(Infobase info) {
        info.setInfoid(GenId.makeSid());
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return (infobaseDAO.insertInfo(info) > 0 ? true : false);
    }

    /**
     * 批量删除信息
     *
     * @param ids
     * @return
     */
    public static int deleteBatch(String ids, Long pid) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("pid", pid);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.deleteInfo(m);
    }

    /**
     * 批量更新发布状态
     *
     * @param ids
     * @param pubstate
     * @return
     */
    public static int updatePubstateBatch(String ids, byte pubstate) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("pubstate", pubstate);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.updatePubstate(m);
    }

    /**
     * 批量更新评论状态
     *
     * @param ids
     * @param iscmt
     * @return
     */
    public static int updateCmtstateBatch(String ids, byte iscmt) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("iscmt", iscmt);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.updateCmtstate(m);
    }

    /**
     * 批量更新标识状态
     *
     * @param ids
     * @param tflag
     * @return
     */
    public static int updateTFlagBatch(String ids, byte tflag) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("tflag", tflag);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.updateTFlag(m);
    }

    /**
     * 批量更新标题颜色
     *
     * @param ids
     * @param tcolor
     * @return
     */
    public static int updateTColorBatch(String ids, String tcolor) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("tcolor", tcolor);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.updateTColor(m);
    }

    /**
     * 批量更新发布时间
     *
     * @param ids
     * @param pubtime
     * @return
     */
    public static int updatePubTimeBatch(String ids, long pubtime) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("pubtime", pubtime);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.updatePubTime(m);
    }

    /**
     * 批量更新截止时间
     *
     * @param ids
     * @param pubtime
     * @return
     */
    public static int updateEndTimeBatch(String ids, long endtime) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("endtime", endtime);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.updateEndTime(m);
    }

    /**
     * 更新信息权重
     *
     * @param infoid
     * @param weight
     * @return
     */
    public static int updateWeight(long infoid, int weight) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("infoid", infoid);
        m.put("weight", weight);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.updateWeight(m);
    }

    /**
     * 批量更新信息频道和类目信息
     *
     * @param ids
     * @param cid
     * @param ctp
     * @return
     */
    public static int updateChannelAndCatalogBatch(String ids, byte channeltype, int cid, String ctp) {
        Map<String, Object> m = new HashMap<String, Object>(4);
        m.put("ida", StringUtil.parseStrToLongArray(ids, StaticMark.STR_LINE));
        m.put("cid", cid);
        m.put("ctp", ctp);
        m.put("channeltype", channeltype);
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return infobaseDAO.updateChannelAndCatalog(m);
    }

    /**
     * 根据查询条件得到信息
     *
     * @param query
     * @return
     */
    public static PageDTO queryByFactor(InfobaseQuery query) {
        InfobaseDAO infobaseDAO = FacadeProxy.getAutoCommitProxy(InfobaseDAO.class);
        return infobaseDAO.queryByFactor(query);
    }

    /**
     * 得到当前信息最大权重值
     *
     * @return
     */
    public static int queryMaxWeight() {
        InfobaseDAO infobaseDAO = FacadeProxy.getAutoCommitProxy(InfobaseDAO.class);
        return infobaseDAO.queryMaxWeight();
    }

    /**
     * 查询信息用于管理员编辑界面展示
     *
     * @param infoid
     * @return
     */
    public static Infobase queryInfobaseForEdit(long infoid) {
        InfobaseDAO infobaseDAO = FacadeProxy.getAutoCommitProxy(InfobaseDAO.class);
        return infobaseDAO.queryInfobaseForEdit(infoid);
    }

    /**
     * 更新信息数据
     *
     * @param info
     * @return
     */
    public static boolean update(Infobase info) {
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return (infobaseDAO.updateInfo(info) > 0 ? true : false);
    }

    // view begin
    /**
     * 查询信息记录,
     *
     * @param infoid
     * @return
     */
    public static Infobase queryInfobaseForView(long infoid) {
        ViewInfoQuery query = new ViewInfoQuery();
        query.setInfoid(infoid);
        query.filter();
        String cacheKey = query.getCacheKey();
        Infobase info = (Infobase) TxCacheManager.get(CacheConst.INFO_VIEW_CACHE_NAME, cacheKey);
        if (info == null) {
            info = FacadeProxy.getAutoCommitProxy(InfobaseDAO.class).queryInfobaseForView(query);
            TxCacheManager.set(CacheConst.INFO_VIEW_CACHE_NAME, 
                    cacheKey, info);
        }
        return info;
    }

    /**
     * 累加信息查看次数
     *
     * @param infoid
     * @return
     */
    public static boolean increaseHitNum(long infoid) {
        InfobaseDAO infobaseDAO = FacadeProxy.getManualCommitProxy(InfobaseDAO.class);
        return (infobaseDAO.increaseHitNum(infoid) > 0 ? true : false);
    }

    /**
     * 前台类别信息列表用于页面板块显示
     *
     * @param viewInfoQuery
     * @return
     */
    public static List<Infobase> listByFactorForView(ViewInfoQuery query) {
        query.filter();
        String cacheKey = query.getCacheKey();
        List<Infobase> list = (List<Infobase>) TxCacheManager.get(CacheConst.INFO_LIST_CACHE_NAME, cacheKey);
        if (list == null) {
            list = FacadeProxy.getAutoCommitProxy(InfobaseDAO.class).listByFactorForView(query);
            if (CollectionUtil.isNotEmpty(list)) {
                TxCacheManager.set(CacheConst.INFO_LIST_CACHE_NAME, 
                        cacheKey, (Serializable) list);
            }
        }
        return list;
    }

    /**
     * 前台info list列表显示
     *
     * @param query
     * @return
     * @throws Exception
     */
    public static List<InfoDTO> listInfoForView(ViewInfoQuery query) {
        query.filter();
        String cacheKey = query.getCacheKey();
        List<InfoDTO> list = (List<InfoDTO>) TxCacheManager.get(CacheConst.INFO_LIST_CACHE_NAME, cacheKey);
        if (list == null) {
            list = FacadeProxy.getAutoCommitProxy(InfobaseDAO.class).listInfoForView(query);
            if (CollectionUtil.isNotEmpty(list)) {
                TxCacheManager.set(CacheConst.INFO_LIST_CACHE_NAME, 
                        cacheKey, (Serializable) list);
            }
        }
        return list;
    }

    /**
     * 前台tag信息列表显示
     *
     * @param query
     * @return
     */
    public static List<?> listByTagsForView(ViewInfoQuery query) {
        query.filter();
        String cacheKey = query.getCacheKey();
        List<?> list = (List<?>) TxCacheManager.get(CacheConst.INFO_TAGS_LIST_CACHE_NAME, cacheKey);
        if (list == null) {
            list = FacadeProxy.getAutoCommitProxy(InfobaseDAO.class).listByTagsForView(query);
            if (CollectionUtil.isNotEmpty(list)) {
                TxCacheManager.set(CacheConst.INFO_TAGS_LIST_CACHE_NAME,
                        cacheKey, (Serializable) list);
            }
        }
        return list;
    }
}